package com.ysy.tz.core.validator;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.function.Function;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;

public class DefaultDecoration {
    private static final Image ERROR_IMAGE = new Image(DefaultDecoration.class.getResource("/images/decoration-error.png").toExternalForm());
    private static final Image WARNING_IMAGE = new Image(DefaultDecoration.class.getResource("/images/decoration-warning.png").toExternalForm());
    private static final String POPUP_SHADOW_EFFECT = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 5, 0, 0, 5);";
    private static final String TOOLTIP_COMMON_EFFECTS = "-fx-font-weight: bold; -fx-padding: 5; -fx-border-width:1;";
    private static final String ERROR_TOOLTIP_EFFECT = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 5, 0, 0, 5);-fx-font-weight: bold; -fx-padding: 5; -fx-border-width:1;-fx-background-color: FBEFEF; -fx-text-fill: cc0033; -fx-border-color:cc0033;";
    private static final String WARNING_TOOLTIP_EFFECT = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 5, 0, 0, 5);-fx-font-weight: bold; -fx-padding: 5; -fx-border-width:1;-fx-background-color: FFFFCC; -fx-text-fill: CC9900; -fx-border-color: CC9900;";
    private static Function<ValidationMessage, Decoration> factory;

    private DefaultDecoration() {
    }

    public static Function<ValidationMessage, Decoration> getFactory() {
        if (factory == null) {
            factory = DefaultDecoration::createGraphicDecoration;
        }

        return factory;
    }

    public static void setFactory(Function<ValidationMessage, Decoration> factory) {
        DefaultDecoration.factory = factory;
    }

    public static Decoration createStyleClassDecoration(ValidationMessage message) {
        return new StyleClassDecoration(new String[]{"validatorfx-" + message.getSeverity().toString().toLowerCase()});
    }

    public static Decoration createGraphicDecoration(ValidationMessage message) {
        return new GraphicDecoration(createDecorationNode(message), Pos.TOP_LEFT);
    }

    private static Node createDecorationNode(ValidationMessage message) {
        ImageView graphic = Severity.ERROR == message.getSeverity() ? createErrorNode() : createWarningNode();
        graphic.getStyleClass().add("shadow_effect");
        Label label = new Label() {
        };
        label.setGraphic(graphic);
        label.setTooltip(createTooltip(message));
        label.setAlignment(Pos.CENTER);
        return label;
    }

    private static Tooltip createTooltip(ValidationMessage message) {
        Tooltip tooltip = new Tooltip(message.getText());
        tooltip.setOpacity(0.9);
        tooltip.setAutoFix(true);
        tooltip.setStyle(Severity.ERROR == message.getSeverity() ? "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 5, 0, 0, 5);-fx-font-weight: bold; -fx-padding: 5; -fx-border-width:1;-fx-background-color: FBEFEF; -fx-text-fill: cc0033; -fx-border-color:cc0033;" : "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 5, 0, 0, 5);-fx-font-weight: bold; -fx-padding: 5; -fx-border-width:1;-fx-background-color: FFFFCC; -fx-text-fill: CC9900; -fx-border-color: CC9900;");
        setTipTime(tooltip, 0, 5000);
        return tooltip;
    }

    public static void setTipTime(Tooltip tooltip, int startTime, int durationTime) {
        try {
            Class tipClass = tooltip.getClass();
            Field f = tipClass.getDeclaredField("BEHAVIOR");
            f.setAccessible(true);
            Class behavior = Class.forName("javafx.scene.control.Tooltip$TooltipBehavior");
            Constructor constructor = behavior.getDeclaredConstructor(Duration.class, Duration.class, Duration.class, Boolean.TYPE);
            constructor.setAccessible(true);
            f.set(behavior, constructor.newInstance(new Duration((double)startTime), new Duration((double)durationTime), new Duration(300.0), false));
        } catch (Exception var7) {
            var7.printStackTrace();
        }

    }

    private static ImageView createErrorNode() {
        return new ImageView(ERROR_IMAGE);
    }

    private static ImageView createWarningNode() {
        return new ImageView(WARNING_IMAGE);
    }
}
